package com.jy.adscm.dealer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.dealer.bean.DealerServiceRelation;
import com.jy.adscm.dealer.mapper.DealerServiceRelationMapper;
import com.jy.adscm.dealer.service.DealerServiceRelationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.serviceProvider.bean.JyService;
import com.jy.adscm.serviceProvider.bean.JyServiceType;
import com.jy.adscm.serviceProvider.mapper.JyServiceTypeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-17
 */
@Service
public class DealerServiceRelationServiceImpl extends ServiceImpl<DealerServiceRelationMapper, DealerServiceRelation> implements DealerServiceRelationService {
    @Autowired
    JyServiceTypeMapper jyServiceTypeMapper;
     @Transactional
    @Override
    public boolean insertDealerServiceRelation(List<DealerServiceRelation> dealerServiceRelations, Token token) throws AdscmSystemException {
        //删除之前所有的关系
        DealerServiceRelation dealerServiceRelation = dealerServiceRelations.get(0);
        Wrapper<DealerServiceRelation> wrapper =new EntityWrapper<>();
        wrapper.eq("did",dealerServiceRelation.getDid());
         boolean delete = super.delete(wrapper);
         for (DealerServiceRelation serviceRelation : dealerServiceRelations) {
            super.insert(serviceRelation);
        }
        return delete;
    }

    @Override
    public List<DealerServiceRelation> selectDealerInfo(Integer did) throws AdscmSystemException {
       Wrapper<DealerServiceRelation> dealerServiceRelationWrapper=new EntityWrapper<>();
       dealerServiceRelationWrapper.eq("did",did);
        List<DealerServiceRelation> dealerServiceRelations = super.selectList(dealerServiceRelationWrapper);
        for (DealerServiceRelation dealerServiceRelation : dealerServiceRelations) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(dealerServiceRelation.getServiceId(),null)){
                JyServiceType jyServiceType = jyServiceTypeMapper.selectById(dealerServiceRelation.getServiceId());
                dealerServiceRelation.setServiceIdName(jyServiceType.getServiceName());
            }
        }
        return dealerServiceRelations;
    }
}
